package com.nikkerball.web.session;

import com.nikkerball.security.Role;
import com.nikkerball.security.SecurityController;
import com.nikkerball.security.impl.DummySecurityController;
import org.apache.wicket.Request;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Marius
 * Date: 16-Jan-2010
 * Time: 11:00:33
 */
public class StumpsSession extends AuthenticatedWebSession {

    // ----- instance fields -----

    private ComputationSession computationSession;

    private SecurityController securityController = new DummySecurityController();

    private String userName;

    // ----- constructors -----

    public StumpsSession(Request request) {
        super(request);
    }

    // ----- public methods -----

    @Override
    public boolean authenticate(String userName, String password) {
        this.userName = userName;
        return securityController.authenticateUser(userName, password);
    }

    public ComputationSession getComputationSession() {
        if (computationSession == null) {
            computationSession = new ComputationSession();
        }
        return computationSession;
    }

    @Override
    public Roles getRoles() {
        if (isSignedIn()) {
            Roles result = new Roles();
            List<Role> roles = securityController.getRolesForUser(this.userName);
            for (Role role : roles) {
                result.add(role.toString());
            }
            return result;
        }
        return null;
    }
}
